#!/fs/home/chakkrit_ta/.local/bin/python
#!/usr/bin/python

import xmlrpclib
import string
import cgi
import cgitb
import xmlrpclib
import json
import re

class Webgraph:
    def __init__(self):
        self.serverproxy = 'http://localhost:9999'
        self.baseurl = 'http://tera.thaigrid.or.th/~chakkrit_ta/webgraph/'
        self.dataset = 'wbCt100Sep08'

    def download(self,filename):
        return '<meta http-equiv="Refresh" content="0; url=%s%s/%s.bin" >' % (self.baseurl,self.dataset,filename)

    def downloadsite(self,filename):
        return '<meta http-equiv="Refresh" content="0; url=%s%s/site/%s.gz" >' % (self.baseurl,self.dataset,filename[7:-1])

    def forward(self,urlrequest):
        proxy = xmlrpclib.ServerProxy(self.serverproxy)
        data = json.loads(str(proxy.forward(urlrequest.strip())))
        return json.dumps(data, sort_keys=True, indent=4)

    def backward(self,urlrequest):
        proxy = xmlrpclib.ServerProxy(self.serverproxy)
        data = json.loads(str(proxy.backward(urlrequest.strip())))
        return json.dumps(data, sort_keys=True, indent=4)

    def urltoid(self,url):
        proxy = xmlrpclib.ServerProxy(self.serverproxy)
        data = json.loads(str(proxy.urltoid(url.strip())))
        return json.dumps(data, sort_keys=True, indent=4)

    def idtourl(self,id):
        proxy = xmlrpclib.ServerProxy(self.serverproxy)
        data = json.loads(str(proxy.idtourl(str(id))))
        return json.dumps(data, sort_keys=True, indent=4)
    
    def list(self,value,maximum):
        proxy = xmlrpclib.ServerProxy(self.serverproxy)
        data = json.loads(str(proxy.list(value,maximum)))
        return json.dumps(data, sort_keys=True, indent=4)

    def stat(self):
        proxy = xmlrpclib.ServerProxy(self.serverproxy)
        data = json.loads(str(proxy.stat()))
        return json.dumps(data, sort_keys=True, indent=4)

if __name__ == '__main__' :
    print 'Content-type: text/html\n'
    cgitb.enable()
    form = cgi.FieldStorage()
    wg = Webgraph()
    gui = False
    if form.has_key('gui') : gui = True
    result = ''
    if form.has_key('service') :
        service = form['service'].value.strip()
        if service == 'download' and form.has_key('filename'):
            filename = form['filename'].value.strip()
            gui = False
            result = wg.download(filename)
        elif service == 'downloadsite' and form.has_key('urlrequest'):
            filename = form['urlrequest'].value.strip()
            gui = False
            result = wg.downloadsite(filename)
        elif service == 'forward' and form.has_key('urlrequest') :
            url = form['urlrequest'].value.strip()
            result = wg.forward(url)
        elif service == 'backward' and form.has_key('urlrequest') :
            url = form['urlrequest'].value.strip()
            result = wg.backward(url)
        elif service == 'site' and form.has_key('urlrequest') :
            url = form['urlrequest'].value.strip()
            result = wg.backward(url)
        elif service == 'urltoid' and form.has_key('url') :
            url = form['url'].value.strip()
            result = wg.urltoid(url)
        elif service == 'idtourl' and form.has_key('id') :
            id= form['id'].value.strip()
            result = wg.idtourl(id)
        elif service == 'list' and form.has_key('value') and form.has_key('maximum') :
            value = form['value'].value.strip()
            maximum = form['maximum'].value.strip()
            result = wg.list(value,maximum)
        elif service == 'stat': 
            result = wg.stat()
        else:
            ans={}
            ans['error'] = True
            ans['message'] = 'Invalid request'
            result = json.dumps(ans)

        if gui :
            print '<textarea rows="30" cols="120">%s</textarea>' % (result)
            print '<h1><a href="%s" style="color:blue; text-decoration:none">Back</a></h1>' % (wg.baseurl)
        else:
            print result

    else:
        ans={}
        ans['error'] = True
        ans['message'] = 'No request'
        print json.dumps(ans, sort_keys=True, indent=4)
